package com.niiwoo.civet.account.service.local;

import com.alibaba.fastjson.JSON;
import com.niiwoo.activity.event.constant.BizEventRabbitConstant;
import com.niiwoo.activity.event.dto.request.AccountEventReqDTO;
import com.niiwoo.activity.event.enums.UserRoleTypeEventEnum;
import com.niiwoo.asset.org.business.constants.OrgRabbitConstant;
import com.niiwoo.asset.org.business.dto.request.callback.EnterpriseOpenAccountNotifyReqDTO;
import com.niiwoo.asset.org.business.enums.CompanyOpenAccountOrderStatusEnum;
import com.niiwoo.civet.account.dao.entity.*;
import com.niiwoo.civet.account.dao.mapper.*;
import com.niiwoo.civet.account.dto.request.OpenCompanyAccountCallbackReqDTO;
import com.niiwoo.civet.account.dto.request.OpenCompanyAccountRequestDTO;
import com.niiwoo.civet.account.enums.AccountCompanyAccountTypeEnum;
import com.niiwoo.civet.account.enums.AccountCompanyAuditStatusEnum;
import com.niiwoo.civet.account.enums.AccountTypeEnum;
import com.niiwoo.civet.account.enums.OpenCompanyAccountOrderAuditStatusEnum;
import com.niiwoo.civet.account.service.local.openAccount2.lanmao.LanMaoHelper;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.lanmao.enums.UserAuthorizationEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.provider.exception.NoSuchEnumValueException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

/**
 * 帐户注册本地服务
 * @author TomXue
 * @date 2018-04-25
 */
@Service
@Slf4j
public class AccountRegisterLocalService {
    @Autowired
    LanMaoSequence lanMaoSequence;
    @Autowired
    AccountRegisterLocalService accountRegisterLocalService;
    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    AccountCompanyMapperExt accountCompanyMapper;
    @Autowired
    private OpenCompanyAccountOrderMapperExt openCompanyAccountOrderMapper;
    @Autowired
    AccountBaseMapperExt accountBaseMapperExt;
    @Autowired
    BankCardMapperExt bankCardMapperExt;
    @Autowired
    BankTypeMapperExt bankTypeMapperExt;
    @Autowired
    PrivacyMasks privacyMasks;
    @Autowired
    private RabbitTemplate consistencyRabbitTemplate;


    @Transactional(isolation = Isolation.READ_COMMITTED)
    public OpenCompanyAccountOrder generateOpenCompanyAccountOrder(OpenCompanyAccountRequestDTO openCompanyAccountRequestDTO){
//        //1.开户类型是否支持?
//        if (!(openCompanyAccountRequestDTO.isCooperateOrgOpenAccount() || openCompanyAccountRequestDTO.isEnterpriseBorrowerOpenAccount() || openCompanyAccountRequestDTO.isGuaranteeOrgOpenAccount())) {
//
//            log.error("企业绑卡注册,机构ID={},开户类型({})暂不支持.", openCompanyAccountRequestDTO.getOrgId(), openCompanyAccountRequestDTO.getOpenAccountWay());
//            throw new RuntimeException(String.format("开户类型(%s)暂不支持",openCompanyAccountRequestDTO.getOpenAccountWay()));
//        }

        AccountCompany accountCompany=accountCompanyMapper.selectByOrgIdAndUserId(openCompanyAccountRequestDTO.getOrgId(),openCompanyAccountRequestDTO.getBorrowOrgId());
        Date now=new Date();
        //2.若机构第一次开户,则生成机构帐户,状态为审核中;
        if(accountCompany==null){
            //新平台编号
            String newAccountNo=String.valueOf(snowflakeIdWorker.nextId());
            //新帐户ID
            Long accountId=snowflakeIdWorker.nextId();
            accountCompany=new AccountCompany();
            accountCompany.setAccountId(accountId);
            accountCompany.setAccountNo(newAccountNo);
            accountCompany.setUserId(openCompanyAccountRequestDTO.getBorrowOrgId());
            accountCompany.setOrgId(openCompanyAccountRequestDTO.getOrgId());
            accountCompany.setCompanyName(openCompanyAccountRequestDTO.getEnterpriseName());
            accountCompany.setBankLicence(openCompanyAccountRequestDTO.getBankLicense());
            accountCompany.setOrgNo(openCompanyAccountRequestDTO.getOrgNo());
            accountCompany.setBusinessLicense(openCompanyAccountRequestDTO.getBusinessLicense());
            accountCompany.setTaxNo(openCompanyAccountRequestDTO.getTaxNo());
            accountCompany.setUnifiedCode(openCompanyAccountRequestDTO.getUnifiedCode());
            accountCompany.setCreditCode(openCompanyAccountRequestDTO.getCreditCode());
            accountCompany.setLegalPersonName(openCompanyAccountRequestDTO.getLegal());
            accountCompany.setLegalPersonIdCard(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getLegalIdCardNo()));
            accountCompany.setIdCardType(openCompanyAccountRequestDTO.getIdCardType());
            accountCompany.setContactName(openCompanyAccountRequestDTO.getContact());
            UserRoleEnum userRoleEnum=getUserRoleByAccountType(AccountCompanyAccountTypeEnum.enumOf(openCompanyAccountRequestDTO.getOpenAccountWay()));
            accountCompany.setUserRole(userRoleEnum.getValue());
            accountCompany.setBankCardNo(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getBankcardNo()));
            accountCompany.setBankCode(openCompanyAccountRequestDTO.getBankcode());
            accountCompany.setAuthList(LanMaoHelper.getAuthTextByRole(userRoleEnum));
            accountCompany.setContactMobileNo(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getContactPhone()));
            accountCompany.setAuditStatus(AccountCompanyAuditStatusEnum.INIT.getStatus());
            accountCompany.setAccountType(openCompanyAccountRequestDTO.getOpenAccountWay());
            accountCompany.setCreateTime(now);

            accountCompanyMapper.insertSelective(accountCompany);
        }

        //2.创建存管企业开户订单;
        OpenCompanyAccountOrder openCompanyAccountOrder=new OpenCompanyAccountOrder();
        openCompanyAccountOrder.setAccountNo(accountCompany.getAccountNo());
        openCompanyAccountOrder.setId(snowflakeIdWorker.nextId());
        openCompanyAccountOrder.setRequestNo(lanMaoSequence.getRequestNo());
        openCompanyAccountOrder.setBorrowOrgId(openCompanyAccountRequestDTO.getBorrowOrgId());
        openCompanyAccountOrder.setOrgId(openCompanyAccountRequestDTO.getOrgId());
        openCompanyAccountOrder.setEnterpriseName(openCompanyAccountRequestDTO.getEnterpriseName());
        openCompanyAccountOrder.setBankLicense(openCompanyAccountRequestDTO.getBankLicense());
        openCompanyAccountOrder.setBusinessLicense(openCompanyAccountRequestDTO.getBusinessLicense());
        openCompanyAccountOrder.setOrgNo(openCompanyAccountRequestDTO.getOrgNo());
        openCompanyAccountOrder.setTaxNo(openCompanyAccountRequestDTO.getTaxNo());
        openCompanyAccountOrder.setUnifiedCode(openCompanyAccountRequestDTO.getUnifiedCode());
        openCompanyAccountOrder.setCreditCode(openCompanyAccountRequestDTO.getCreditCode());
        openCompanyAccountOrder.setLegal(openCompanyAccountRequestDTO.getLegal());
        openCompanyAccountOrder.setIdCardType(openCompanyAccountRequestDTO.getIdCardType());
        openCompanyAccountOrder.setLegalIdCardNo(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getLegalIdCardNo()));
        openCompanyAccountOrder.setContact(openCompanyAccountRequestDTO.getContact());
        openCompanyAccountOrder.setContactPhone(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getContactPhone()));
        //合作机构开通权限：自动代偿
        if(openCompanyAccountRequestDTO.isCooperateOrgOpenAccount()) {
            openCompanyAccountOrder.setUserRole(UserRoleEnum.COLLABORATOR.getValue());
            openCompanyAccountOrder.setAuthlist(LanMaoHelper.getAuthTextByRole(UserRoleEnum.COLLABORATOR));
        }
        //借款人开通权限：授权自动还款，授权自动提现
        else if(openCompanyAccountRequestDTO.isEnterpriseBorrowerOpenAccount()){
            openCompanyAccountOrder.setUserRole(UserRoleEnum.BORROWERS.getValue());

            Set<String> authSet = LanMaoHelper.getAuthSetByRole(UserRoleEnum.BORROWERS);
            authSet.remove(UserAuthorizationEnum.RECHARGE.name());
            openCompanyAccountOrder.setAuthlist(String.join(",", authSet));
        }
        //担保机构开通权限：自动代偿
        else if(openCompanyAccountRequestDTO.isGuaranteeOrgOpenAccount()){
            openCompanyAccountOrder.setUserRole(UserRoleEnum.GUARANTEECORP.getValue());
            openCompanyAccountOrder.setAuthlist(LanMaoHelper.getAuthTextByRole(UserRoleEnum.GUARANTEECORP));
        }
        //企业投资人开通权限：自动投标
        else if(openCompanyAccountRequestDTO.isInvestOrgOpenAccount()){
            openCompanyAccountOrder.setUserRole(UserRoleEnum.INVESTOR.getValue());
            openCompanyAccountOrder.setAuthlist(LanMaoHelper.getAuthTextByRole(UserRoleEnum.INVESTOR));
        }
        openCompanyAccountOrder.setBankCardNo(privacyMasks.encryptPrivacy(openCompanyAccountRequestDTO.getBankcardNo()));
        openCompanyAccountOrder.setBankCode(openCompanyAccountRequestDTO.getBankcode());
        openCompanyAccountOrder.setRedirectUrl(openCompanyAccountRequestDTO.getRedirectUrl());
        openCompanyAccountOrder.setStatus(OpenCompanyAccountOrderAuditStatusEnum.INIT.getStatus());
        openCompanyAccountOrder.setCreateTime(now);

        openCompanyAccountOrderMapper.insertSelective(openCompanyAccountOrder);

        return openCompanyAccountOrder;
    }

    private UserRoleEnum getUserRoleByAccountType(AccountCompanyAccountTypeEnum accountCompanyAccountTypeEnum) {
        switch (accountCompanyAccountTypeEnum) {
            //企业借款人
            case BORROW_ORG:
                return UserRoleEnum.BORROWERS;
            //合作机构
            case COOPERATE_ORG:
                return UserRoleEnum.COLLABORATOR;
            //担保机构
            case GUARANTEE_ORG:
                return UserRoleEnum.GUARANTEECORP;
            case INVEST_ORG:
                return UserRoleEnum.INVESTOR;
        }
        throw new NoSuchEnumValueException("暂不支持该帐户类型(".concat(accountCompanyAccountTypeEnum == null ? null : accountCompanyAccountTypeEnum.getDesc()).concat(")开户"));
    }

    /**
     * 企业绑卡注册[审核中状态回调]
     * @param openCompanyAccountCallbackReqDTO
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void openCompanyAccountCallBackAuditIng(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO){
        log.info("企业绑卡注册回调,请求流水号={},审核中回调处理.",openCompanyAccountCallbackReqDTO.getRequestNo());
        //1.修改订单状态由初始化-->审核中;
        openCompanyAccountOrderMapper.updateStatusByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo(),OpenCompanyAccountOrderAuditStatusEnum.AUDIT_ING.getStatus());
        OpenCompanyAccountOrder openCompanyAccountOrder=openCompanyAccountOrderMapper.selectByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo());
        //2.修改企业帐户状态由初始化-->审核中;
        accountCompanyMapper.updateAuditStatusByAccountNo(openCompanyAccountOrder.getAccountNo(),AccountCompanyAuditStatusEnum.AUDIT_ING.getStatus());
    }

    /**
     * 获取机构账户信息
     *
     * @param openCompanyAccountCallbackReqDTO 存管开户回调request
     * @return accountCompany
     */
    public AccountCompany getAccountCompanyFromCallBack(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO) {
        OpenCompanyAccountOrder openCompanyAccountOrder=openCompanyAccountOrderMapper.selectByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo());
        return accountCompanyMapper.selectByAccountNo(openCompanyAccountOrder.getAccountNo());
    }

    /**
     * 企业绑卡注册[审核通过状态回调]
     * @param openCompanyAccountCallbackReqDTO
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void openCompanyAccountCallBackAuditPass(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO){
        log.info("企业绑卡注册回调,请求流水号={},审核通过回调处理.",openCompanyAccountCallbackReqDTO.getRequestNo());
        OpenCompanyAccountOrder openCompanyAccountOrder=openCompanyAccountOrderMapper.selectByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo());
        AccountCompany accountCompany=accountCompanyMapper.selectByAccountNo(openCompanyAccountOrder.getAccountNo());
        //1.修改订单状态;
        openCompanyAccountOrderMapper.updateStatusByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo(),OpenCompanyAccountOrderAuditStatusEnum.AUDIT_PASS.getStatus());
        //2.通过审核成功的开户订单资料，根据平台帐户编号进行资料覆盖更新，以确保信息一致，并且更新审核状态由审核中-->审核通过;
        AccountCompany accountCompany2Update=new AccountCompany();
        accountCompany2Update.setAccountNo(openCompanyAccountOrder.getAccountNo());
        accountCompany2Update.setUserId(openCompanyAccountOrder.getBorrowOrgId());
        accountCompany2Update.setOrgId(openCompanyAccountOrder.getOrgId());
        accountCompany2Update.setCompanyName(openCompanyAccountOrder.getEnterpriseName());
        accountCompany2Update.setBankLicence(openCompanyAccountOrder.getBankLicense());
        accountCompany2Update.setOrgNo(openCompanyAccountOrder.getOrgNo());
        accountCompany2Update.setBusinessLicense(openCompanyAccountOrder.getBusinessLicense());
        accountCompany2Update.setTaxNo(openCompanyAccountOrder.getTaxNo());
        accountCompany2Update.setUnifiedCode(openCompanyAccountOrder.getUnifiedCode());
        accountCompany2Update.setCreditCode(openCompanyAccountOrder.getCreditCode());
        accountCompany2Update.setLegalPersonName(openCompanyAccountOrder.getLegal());
        accountCompany2Update.setIdCardType(openCompanyAccountOrder.getIdCardType());
        accountCompany2Update.setLegalPersonIdCard(openCompanyAccountOrder.getLegalIdCardNo());
        accountCompany2Update.setContactMobileNo(openCompanyAccountOrder.getContactPhone());
        accountCompany2Update.setContactName(openCompanyAccountOrder.getContact());
        accountCompany2Update.setUserRole(openCompanyAccountOrder.getUserRole());
        accountCompany2Update.setBankCardNo(openCompanyAccountOrder.getBankCardNo());
        accountCompany2Update.setBankCode(openCompanyAccountOrder.getBankCode());
        accountCompany2Update.setAuthList(openCompanyAccountOrder.getAuthlist());
        accountCompany2Update.setAuditStatus(AccountCompanyAuditStatusEnum.AUDIT_PASS.getStatus());
        accountCompany2Update.setUpdateTime(new Date());

        accountCompanyMapper.updateByAccountNo(accountCompany2Update);

        Date now=new Date();
        //3.新增account_base;
        AccountBase accountBase=new AccountBase();
        accountBase.setId(accountCompany.getAccountId());
        accountBase.setUserId(accountCompany.getUserId());
        accountBase.setAccountRole(accountCompany.getUserRole());
        accountBase.setAccountNo(accountCompany.getAccountNo());
        accountBase.setAvailableAmount(BigDecimal.ZERO);
        accountBase.setImportUserActiva((byte)1);

        // 担保机构：担保公司
        if (AccountCompanyAccountTypeEnum.GUARANTEE_ORG.getType().equals(accountCompany.getAccountType())){
            accountBase.setAccountRole(UserRoleEnum.GUARANTEECORP.getValue());
            accountBase.setCompensatory((byte)1);
        }
        // 合作机构：鸿特，土巴兔等
        else if (isCooperateOrg(accountCompany.getAccountType())) {
            accountBase.setAccountRole(UserRoleEnum.COLLABORATOR.getValue());
            accountBase.setCompensatory((byte)1);
        }
        // 企业借款人：鸿特旗下企业借款人，土巴兔旗下装修公司
        else if (isBorrowOrg(accountCompany.getAccountType())) {
            accountBase.setAccountRole(UserRoleEnum.BORROWERS.getValue());
            accountBase.setRepayment((byte)1);
            accountBase.setWithdraw((byte)1);
        }
        // 企业投资人：自动投标
        else if(isInvestOrg(accountCompany.getAccountType())){
            accountBase.setAccountRole(UserRoleEnum.INVESTOR.getValue());
            accountBase.setTender((byte)1);
        }
        accountBase.setAccountType(AccountTypeEnum.ENTERPRISE.getValue());
        accountBase.setOrgId(accountCompany.getOrgId());
        accountBase.setIsShowBill((byte) 1);
        accountBase.setCreateTime(now);

        accountBaseMapperExt.insertSelective(accountBase);

        //4.新增银行卡;
        BankCard bankCard=new BankCard();
        bankCard.setId(snowflakeIdWorker.nextId());
        bankCard.setUserId(openCompanyAccountOrder.getBorrowOrgId());
        bankCard.setAccountId(accountCompany.getAccountId());
        bankCard.setAccountType(AccountTypeEnum.ENTERPRISE.getValue());
        BankType bankType=bankTypeMapperExt.selectBankTypeByCode(openCompanyAccountOrder.getBankCode());
        bankCard.setBankTypeId(bankType.getId());
        bankCard.setBankCode(openCompanyAccountOrder.getBankCode());
        bankCard.setBankAccountNo(openCompanyAccountOrder.getBankCardNo());
        bankCard.setRealName(openCompanyAccountOrder.getLegal());
        bankCard.setIdCard(openCompanyAccountOrder.getLegalIdCardNo());
        bankCard.setMobileNo(openCompanyAccountOrder.getContactPhone());
        bankCard.setType((byte)2);
        bankCard.setCreateTime(now);

        bankCardMapperExt.insertSelective(bankCard);

        //5.通知机构开户成功;
        EnterpriseOpenAccountNotifyReqDTO enterpriseOpenAccountNotifyReqDTO=new EnterpriseOpenAccountNotifyReqDTO();
        enterpriseOpenAccountNotifyReqDTO.setMessage(CompanyOpenAccountOrderStatusEnum.SUCCESS.getDesc());
        enterpriseOpenAccountNotifyReqDTO.setOrgId(openCompanyAccountOrder.getOrgId());
        enterpriseOpenAccountNotifyReqDTO.setCharterCode(openCompanyAccountOrder.getBusinessLicense());
        enterpriseOpenAccountNotifyReqDTO.setStatus(CompanyOpenAccountOrderStatusEnum.SUCCESS.getStatus());
        ((AccountRegisterLocalService)AopContext.currentProxy()).sendMQ2NotifyOrg(enterpriseOpenAccountNotifyReqDTO);

        //6.企业投资人与企业借款人,则开户成功后通知活动;
        if (isInvestOrg(accountCompany.getAccountType()) || isBorrowOrg(accountCompany.getAccountType())) {
            ((AccountRegisterLocalService) AopContext.currentProxy()).sendMQ2Activity(openCompanyAccountOrder);
        }
    }

    /**
     * 合作机构?
     * @param accountType
     * @return
     */
    private boolean isCooperateOrg(Byte accountType){
        return null != accountType && accountType.equals(AccountCompanyAccountTypeEnum.COOPERATE_ORG.getType());
    }

    /**
     * 企业投资人?
     * @param accountType
     * @return
     */
    private boolean isInvestOrg(Byte accountType){
        return null != accountType && accountType.equals(AccountCompanyAccountTypeEnum.INVEST_ORG.getType());
    }

    /**
     * 企业借款人?
     * @param accountType
     * @return
     */
    private boolean isBorrowOrg(Byte accountType){
        return null != accountType && accountType.equals(AccountCompanyAccountTypeEnum.BORROW_ORG.getType());
    }

    /**
     * 企业绑卡注册[审核拒绝状态回调]
     * @param openCompanyAccountCallbackReqDTO
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void openCompanyAccountCallBackAuditRefuse(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO){
        log.info("企业绑卡注册回调,请求流水号={},审核拒绝回调处理.",openCompanyAccountCallbackReqDTO.getRequestNo());
        OpenCompanyAccountOrder openCompanyAccountOrder=openCompanyAccountOrderMapper.selectByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo());
        //1.修改开户订单状态由审核中-->审核拒绝;
        openCompanyAccountOrderMapper.updateStatusByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo(),OpenCompanyAccountOrderAuditStatusEnum.AUDIT_REFUSE.getStatus());

        //2.修改公司帐户状态由审核中-->审核拒绝;
        accountCompanyMapper.updateAuditStatusByAccountNo(openCompanyAccountOrder.getAccountNo(),AccountCompanyAuditStatusEnum.AUDIT_REFUSE.getStatus());

        //3.通知机构开户失败;
        EnterpriseOpenAccountNotifyReqDTO enterpriseOpenAccountNotifyReqDTO=new EnterpriseOpenAccountNotifyReqDTO();
        enterpriseOpenAccountNotifyReqDTO.setMessage(CompanyOpenAccountOrderStatusEnum.FAILURE.getDesc());
        enterpriseOpenAccountNotifyReqDTO.setOrgId(openCompanyAccountOrder.getOrgId());
        enterpriseOpenAccountNotifyReqDTO.setCharterCode(openCompanyAccountOrder.getBusinessLicense());
        enterpriseOpenAccountNotifyReqDTO.setStatus(CompanyOpenAccountOrderStatusEnum.FAILURE.getStatus());
        ((AccountRegisterLocalService)AopContext.currentProxy()).sendMQ2NotifyOrg(enterpriseOpenAccountNotifyReqDTO);
    }

    /**
     * 企业绑卡注册[审核退回状态回调]
     * @param openCompanyAccountCallbackReqDTO
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void openCompanyAccountCallBackAuditBack(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO){
        log.info("企业绑卡注册回调,请求流水号={},审核退回回调处理.",openCompanyAccountCallbackReqDTO.getRequestNo());
        OpenCompanyAccountOrder openCompanyAccountOrder=openCompanyAccountOrderMapper.selectByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo());
        //1.修改开户订单状态由审核中-->审核退回;
        openCompanyAccountOrderMapper.updateStatusByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo(),OpenCompanyAccountOrderAuditStatusEnum.AUDIT_BACK.getStatus());

        //2.修改公司帐户状态由审核中-->审核退回;
        accountCompanyMapper.updateAuditStatusByAccountNo(openCompanyAccountOrder.getAccountNo(),AccountCompanyAuditStatusEnum.AUDIT_BACK.getStatus());
    }

    /**
     * 企业绑卡注册[存管订单处理失败回调]
     * @param openCompanyAccountCallbackReqDTO
     */
    public void openCompanyAccountCallBackError(OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO){
        log.info("企业绑卡注册回调,请求流水号={},存管订单处理失败回调.",openCompanyAccountCallbackReqDTO.getRequestNo());
        //1.修改开户订单状态由审核中-->存管订单处理失败;
        openCompanyAccountOrderMapper.updateStatusByRequestNo(openCompanyAccountCallbackReqDTO.getRequestNo(),OpenCompanyAccountOrderAuditStatusEnum.BANK_ERROR.getStatus());
    }

    /**
     * 企业开户结果通知机构
     * @param enterpriseOpenAccountNotifyReqDTO
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void sendMQ2NotifyOrg(EnterpriseOpenAccountNotifyReqDTO enterpriseOpenAccountNotifyReqDTO){
        consistencyRabbitTemplate.convertAndSend(OrgRabbitConstant.Exchange.ORG_COMMON_DIRECT_EXCHANGE, OrgRabbitConstant.getRoutingKey(
                OrgRabbitConstant.Exchange.ORG_COMMON_DIRECT_EXCHANGE, OrgRabbitConstant.Queue.ENTERPRISE_OPEN_ACCOUNT_CALLBACK_QUEUE),
                enterpriseOpenAccountNotifyReqDTO);
    }

    /**
     * 企业开户结果通知活动
     * @param openCompanyAccountOrder
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void sendMQ2Activity(OpenCompanyAccountOrder openCompanyAccountOrder){
        Date now=new Date();
        AccountEventReqDTO accountEventReqDTO=new AccountEventReqDTO();
        accountEventReqDTO.setUserId(openCompanyAccountOrder.getBorrowOrgId());
        accountEventReqDTO.setUpdateDate(now);
        accountEventReqDTO.setOpenAccountDate(now);
        accountEventReqDTO.setUserRoleTypeEventEnum(UserRoleTypeEventEnum.valueOf(openCompanyAccountOrder.getUserRole()));
        log.error("企业绑卡注册,机构ID={},通知活动MQ请求参数={}", openCompanyAccountOrder.getBorrowOrgId(), JSON.toJSONString(accountEventReqDTO));
        consistencyRabbitTemplate.convertAndSend(BizEventRabbitConstant.Exchange.BIZ_EVENT_DIRECT_EXCHANGE, BizEventRabbitConstant.Key.BIZ_EVENT_ACCOUNT_KEY,
                accountEventReqDTO);
    }
}
